package com.tenbent.product.base.query;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tenbent.product.base.query.page.PageAdapter;
import com.tenbent.product.base.query.page.PageResult;
import com.tenbent.product.base.query.page.Pageable;

import java.util.Map;

/**
 * 分页执行器
 *
 * @author Randy
 */
public class PageQuery {

	public static <T> PageResult<T> executeMapQuery(Pageable pageable, QueryMapMethod<T> method) {
		PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize());
		Page<T> page = method.query(pageable.getParamMap());
		PageAdapter<T> adapter = new PageAdapter<>(page, pageable);
		return adapter.getPage();
	}

	public static <T> PageResult<T> executeObjQuery(Pageable pageable, QueryObjMethod<T> method) {
		PageHelper.startPage(pageable.getPageNumber(), pageable.getPageSize());
		Page<T> page = method.query((T) pageable.getParamBizObject());
		PageAdapter<T> adapter = new PageAdapter<>(page, pageable);
		return adapter.getPage();
	}

	public interface QueryMapMethod<T> {
		Page<T> query(Map<String, Object> paramMap);
	}

	public interface QueryObjMethod<T> {
		Page<T> query(T obj);
	}
}
